package common;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import domain.User;
import service.LoginServise;

@WebServlet(urlPatterns="/user/login.do")
public class LoginServlet extends HttpServlet{

	private static final long serialVersionUID = 8824479938317206643L;
    private LoginServise loginservise =new LoginServise();
    private Logger logger=LoggerFactory.getLogger(getClass());
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		 doPost(req, resp);
		
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			//1.抽取参数
			String usercode=req.getParameter("usercode");
			String passwords=req.getParameter("passwords");
			//2.判断一下下
			if(usercode==null||usercode.length()<6) {
				throw new CommException("账号不能为空不能少于六位数");
			}
			if(passwords==null||passwords.length()<6) {
				throw new CommException("密码不能为空");
			}
			logger.debug("校验成功");
			//3.封装一下下
			User user=new User();
			user.setUsercode(usercode);
			user.setPasswords(passwords);
			//调用业务
			User users= loginservise.login(user);
			HttpSession session =req.getSession();
			session.setAttribute("users", users);
			resp.sendRedirect(req.getContextPath() + "/index.jsp");
			
		} catch (Exception e) {
			req.setAttribute("eorr", e.getMessage());
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		}
	}

}
